package com.one.consumer.user.ui;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.utils.ClickUtils;
import com.one.common.utils.Logger;
import com.one.common.utils.StringUtils;
import com.one.common.view.widget.InputLayout;
import com.one.common.view.widget.MyTitleBar;
import com.one.consumer.BaseActivityC;
import com.one.consumer.R;
import com.one.consumer.common.extra.LocationExtra;
import com.one.consumer.user.model.extra.AddressExtra;
import com.one.consumer.user.model.item.AddressItem;
import com.one.consumer.user.presenter.AddAndUpdateAddressPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/28 5:12 PM
 * description ：
 **/
@Route(path = RouterPath.ADD_ADDRESS)
public class AddAddressActivity extends BaseActivityC<AddAndUpdateAddressPresenter> {
    @BindView(R.id.il_contact)
    InputLayout ilContact;
    @BindView(R.id.il_phone)
    InputLayout ilPhone;
    @BindView(R.id.il_city)
    InputLayout ilCity;
    @BindView(R.id.il_address)
    InputLayout ilAddress;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    private AddressExtra mExtra;
    private LocationExtra locationExtra;

    @Override
    protected void initPresenter() {
        mPresenter = new AddAndUpdateAddressPresenter(this, this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_add_address;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        mExtra = (AddressExtra) getIntent().getSerializableExtra(AddressExtra.getExtraName());
        String title = "添加地址";
        if (mExtra != null) {
            if (mExtra.getType() == AddressExtra.UPDATE) {
                title = "修改地址";
            }
        }
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText(title);

    }

    @Override
    public void initView() {
        super.initView();
        ilCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 选择地址
                RouterManager.getInstance().goForResult(AddAddressActivity.this,
                        RouterPath.SELECT_LOCATION);
            }
        });

        if (mExtra != null && mExtra.getAddressItem() != null) {
            setAddressData(mExtra.getAddressItem());
            tvSubmit.setText("修改地址");
        }
    }

    private void setAddressData(AddressItem item) {
        ilContact.getEditView().setText(item.getAddress_name());
        ilPhone.getEditView().setText(item.getAddress_mobile());
        ilCity.setText(item.getAddress_province()+item.getAddress_city()+item.getAddress_country());
        ilAddress.getEditView().setText(item.getAddress_detailed());

        locationExtra = new LocationExtra();
        locationExtra.setTitle(item.getAddress_detailed());
        locationExtra.setLatitude(StringUtils.getFloatToString(item.getAddress_latitude()));
        locationExtra.setLongitude(StringUtils.getFloatToString(item.getAddress_longitude()));
    }

    @OnClick(R.id.tv_submit)
    public void submit() {
        if(ClickUtils.isFastClick())return;
        mPresenter.submit(ilContact.getText(), ilPhone.getText(),
                ilAddress.getText(), locationExtra);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RouterManager.RESULT_REQUEST_CODE_DEFAULT && data != null) {
            // 地址
            locationExtra = (LocationExtra) data.getSerializableExtra(LocationExtra.getExtraName());
            Logger.d("地址" + requestCode);
            if (locationExtra != null) {
                ilCity.getTvCenter().setText(locationExtra.getAddress());
                Logger.d("地址" + locationExtra.toString());
            }
        }
    }

}
